/**
 * @description 交易对手基础信息
 * @author chenyan
 */

import { connect } from 'dva';
import React, { PureComponent } from 'react';
import { TableC, SecondHeaderC } from '../../../comps';
import { getProductName } from '../../../utils/utils';
class PersonalBWdetailC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      businessKey: undefined,
      timeStamp: undefined,
      columns: undefined,
      dataSource: undefined, //列表
      currentPage: 1, //页码
      numInPage: 20, //每页条数
      pageInfo: {}, //后端返回的页码信息
      productList: [], //产品组下拉
    };
  }

  componentDidMount() {
    this.setState({
      businessKey: this.props.businessKey,
      timeStamp: this.props.timeStamp,
    });
    //获取产品组数据 用处理表格的产品组 名称
    this.props.dispatch({
      type: 'productAGroupM/getProductGroups',
      payload: {},
      callback: (e) => {
        this.setState({
          productList: e.groupsList ? [...e.groupsList] : [], // 列表下拉数据
          columns: this.state.columns ? [...this.state.columns] : [],
        });
      },
    });
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.state.timeStamp !== nextProps.timeStamp) {
      this.setState(
        {
          businessKey: nextProps.businessKey,
          timeStamp: nextProps.timeStamp,
        },
        () => {
          this.queryDetail();
        },
      );
    }
  }
  //    //查询黑白名单 交易对手信息
  queryDetail = () => {
    const { numInPage, currentPage } = this.state;
    this.setState({
      columns: undefined,
      dataSource: undefined, //产品信息
      pageInfo: {},
    }); //查询之前清除数据
    this.props.dispatch({
      //查询详情数据
      type: 'personalBWdetailM/getBWList',
      payload: {
        id: this.state.businessKey,
        numInPage,
        currentPage,
      },
      callback: (type, data) => {
        if (type == 'success') {
          data.dataSource.map((item, key) => {
            item.key = key;
          });

          this.setState({
            columns: data.columns || [],
            dataSource: data.dataSource || [],
            pageInfo: data.pageInfo || {},
          });
        } else {
          this.setState({
            columns: [],
            dataSource: [],
            pageInfo: {},
          });
        }
      },
    });
  };
  render() {
    const { loading } = this.props;
    const { columns, dataSource } = this.state;
    columns &&
      columns.map((item, key) => {
        item.dataIndex = item.key;
        if (item.key == 'portfolioName') {
          item.render = (text, record) => {
            //找出产品组名称 原因是在资管，交易对手一个数据库，基础信息（包含用户信息、产品、组合、产品组等）一个库，两个库还不一样，所以数据没有共享

            return getProductName(record.portfolioId, this.state.productList) || '--';
          };
        }
      });
    return (
      <div>
        <SecondHeaderC title={'交易对手信息'} />
        <TableC columns={columns} dataSource={dataSource} hidePage={true} rowKey='key' />
      </div>
    );
  }
}

export default connect(({ personalBWdetailM, productAGroupM, loading }) => ({ personalBWdetailM, productAGroupM, loading }))(PersonalBWdetailC);
